package com.gtw.modules.api.controller;

import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbSignDataService;
import com.gtw.modules.api.service.TbUserService;
import com.gtw.modules.entity.TbSignData;
import com.gtw.modules.entity.TbUser;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/** 用户签到
 * Created by wuye on 2018/3/29.
 */
@RestController
@RequestMapping("/api/sing")
public class TbUserSingController {

    @Autowired
    private TbSignDataService tbSignDataService;

    @Autowired
    private TbUserService tbUserService;


    /**
     * 查询当前用户签到日期
     * @return
     */
    @RequestMapping("/queryDate")
    public R queryDate(@LoginUserApi TbUser tbUser){
        TbSignData tbSignData = tbSignDataService.foundByUserId(tbUser.getUserId());
        return R.ok().put("data",tbSignData);
    }

    /**
     * 签到
     * @return
     */
    @RequestMapping("/doSing")
    public R doSing(@LoginUserApi TbUser tbUser){
        TbSignData tbSignData = tbSignDataService.foundByUserId(tbUser.getUserId());
        if(tbSignData== null){
            //添加签到信息
            TbSignData signData = new TbSignData();
            signData.setSigndate(new Date());
            signData.setUserid(tbUser.getUserId());
            signData.setSignnum(1);
            tbSignDataService.insert(signData);
            tbUser.setCharmvalue(tbUser.getCharmvalue()+1);
            tbUserService.update(tbUser);
            return R.ok().put("data",signData);
        }else{
            //判断今天是否签到
            //判断是否断签，断签——续签积累数清零
            //判断当前时间是否大于签到日期一天
            Map<String, Object> map = new HashedMap();
            map.put("oldDate",tbSignData.getSigndate());
            map.put("nowDate",new Date());
            int result = tbSignDataService.datediff(map);
            if(result == 1) {
                //修改签到信息
                tbSignData.setSigndate(new Date());
                tbSignData.setSignnum(tbSignData.getSignnum() + 1);
                tbSignDataService.updateByPrimaryKey(tbSignData);
                tbUser.setCharmvalue(tbUser.getCharmvalue()+tbSignData.getSignnum());
                tbUserService.update(tbUser);
                return R.ok().put("data",tbSignData);
            }
            else if(result == 0){
                return R.ok().put("msg","今天已经签过到了");
            }
            else{
                //修改签到信息
                tbSignData.setSigndate(new Date());
                tbSignData.setSignnum(1);
                tbSignDataService.updateByPrimaryKey(tbSignData);
                tbUser.setCharmvalue(tbUser.getCharmvalue()+1);
                tbUserService.update(tbUser);
                return R.ok().put("data",tbSignData);
            }
        }

    }
}
